﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 坦克大战.Properties;

namespace 坦克大战
{
    
    internal class EnemtTank:Movingthing
    {
        public int ChangeDirSpeed { get; set; }
        public int ChangeDirCount { get; set; }
        public int AttackSpeed { get; set; }
        private int AttackCount = 0;
        private Random rd = new Random();
        public EnemtTank(int x, int y, int speed,Bitmap bmpDown, Bitmap bmpUp, Bitmap bmpLeft, Bitmap bmpRight)
        {

            
            this.X = x;
            this.Y = y;
            this.Speed = speed;
            this.BitmapLeft = bmpLeft;
            this.BitmapUp = bmpUp;
            this.BitmapRight = bmpRight;
            this.BitmapDown = bmpDown;
            this.Dir = Dirction.Down;
            AttackSpeed = 60;
            ChangeDirSpeed = 70;



        }

        public override void Update()
        {
            MoveCheck();//移动检查
            Move();
            AttackCheck();
            base.Update();
            AutoChangeDir();

        }

        private void AutoChangeDir()
        {
            ChangeDirCount++;
            if(ChangeDirCount<ChangeDirSpeed) return;
            ChangeDirection();
            ChangeDirCount = 0;
        }

        private void MoveCheck()
        {

            #region 检查有没有超过窗体边界
            if (Dir == Dirction.Up)
            {
                if (Y - Speed < 0)
                {
                    ChangeDirection();  return;
                }
            }
            else if (Dir == Dirction.Down)
            {
                if (Y + Speed + Height > 450)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Dirction.Left)
            {
                if (X - Speed < 0)
                {
                    ChangeDirection(); return;
                }
            }
            else if (Dir == Dirction.Right)
            {
                if (X + Speed + Width > 450)
                {
                    ChangeDirection(); return;
                }
            }
            #endregion


            //检查有没有和其他元素发生碰撞

            Rectangle rect = GetRectangle();

            switch (Dir)
            {
                case Dirction.Up:
                    rect.Y -= Speed;
                    break;
                case Dirction.Down:
                    rect.Y += Speed;
                    break;
                case Dirction.Left:
                    rect.X -= Speed;
                    break;
                case Dirction.Right:
                    rect.X += Speed;
                    break;
            }

            if (GameObjectManager.IsCollidedWall(rect) != null)
            {
                ChangeDirection(); return;
            }
            if (GameObjectManager.IsCollidedSteel(rect) != null)
            {
                ChangeDirection(); return;
            }
            if (GameObjectManager.IsCollidedBoss(rect))
            {
                ChangeDirection(); return;
            }
        }
        private void ChangeDirection()
        {
            //4
            //Random rd = new Random();
            while(true)
            {
                Dirction dir = (Dirction)rd.Next(0, 4);
                if (dir==Dir)
                {
                   continue;
                }
                {
                    Dir=dir;
                    break;
                }
            }
            MoveCheck();
            


        }


        private void Move()
        {
            

            switch (Dir)
            {
                case Dirction.Up:
                    Y -= Speed;
                    break;
                case Dirction.Down:
                    Y += Speed;
                    break;
                case Dirction.Left:
                    X -= Speed;
                    break;
                case Dirction.Right:
                    X += Speed;
                    break;
            }
        }

        private void AttackCheck()
        {
            AttackCount++;
            if (AttackCount < AttackSpeed) return;
            Attack();
            AttackCount = 0;


        }


        private void Attack()
        {
            int x = this.X;
            int y = this.Y;

            switch (Dir)
            {
                case Dirction.Up:
                    x += Width / 2;
                    break;
                case Dirction.Down:
                    x += Width / 2;
                    y += Height;
                    break;
                case Dirction.Left:
                    y += Height / 2;
                    break;
                case Dirction.Right:
                    x += Width;
                    y += Height / 2;
                    break;


            }



            GameObjectManager.CreateBullet(x, y, Tag.EnemyTnak, Dir);
        }

    }
}
